package com.jdk.asm;

import javassist.*;

import java.io.IOException;

/**
 * @author jukg
 * @date 2023/08/17 14:21
 * @description
 */
public class ASMDemo {

    public static void main(String[] args) throws NotFoundException, CannotCompileException, IOException, IllegalAccessException, InstantiationException {
        ClassPool pool = ClassPool.getDefault();
        CtClass ct = pool.get("com.jdk.asm.ASMDemo");
        CtMethod cm = ct.getDeclaredMethod("say");
        cm.insertBefore("{System.out.println(\"before.....\");}");
        cm.insertAfter("{System.out.println(\"after.....\");}");
        ct.writeFile("target/classes/com/jdk/asm/ASMDemo.class");
        Class clz = ct.toClass();
        ASMDemo asmDemo = (ASMDemo) clz.newInstance();
        asmDemo.say();
    }

    public void say(){
        System.out.println("to say.....");
    }
}
